/**
 *     This file is part of cesm-ws.
 *
 *     cesm-ws, Web Service for Community Earth System Model
 *
 *     Copyright (c) 2010, Purdue University and/or affilicates or thrid-party contributors as
 *     indicated by the @author tags or express copyright attribution statements
 *     applied by the authors. All third-party contributions are distributed under
 *     license by Purdue University.
 *
 *     cesm-ws is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this distribution.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.purdue.rcac.grid.submit.globus;
import org.apache.log4j.Logger;
import org.globus.gram.GramJob;

 

class InteractiveJobListener extends JobListener{

	private boolean quiet;
    private boolean finished = false;
    
	/**
	 * Log4j logger
	 */
	private static final Logger logger = Logger.getLogger(InteractiveJobListener.class
			.getName());
	
    public InteractiveJobListener(boolean quiet) {
        this.quiet = quiet;
    }

    public boolean stillActive() {
        return !this.finished;
    }

    // waits for DONE or FAILED status
    public synchronized void waitFor()
        throws InterruptedException {
    	logger.debug("before while loop");
        while (!finished) {
            wait();
        }
    }

    public synchronized void statusChanged(GramJob job) {
        if (!quiet) {
            logger.debug("Job Status changed to: "+ job.getStatusAsString());
        }
        status = job.getStatus();
        if (status == GramJob.STATUS_DONE) {
            finished = true;
            error = 0;
            notify();
        } else if (job.getStatus() == GramJob.STATUS_FAILED) {
            finished = true;
            error = job.getError();
            notify();
        }
    }
}

